package com.ruoyi.web.controller.production.regulator;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.regulator.RegulatorName;
import com.ruoyi.production.service.regulator.IRegulatorNameService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 调节剂名称Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/production/regulatorName")
public class RegulatorNameController extends BaseController
{
    @Autowired
    private IRegulatorNameService regulatorNameService;

    /**
     * 查询调节剂名称列表
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:list')")
    @GetMapping("/list")
    public TableDataInfo list(RegulatorName regulatorName)
    {
        startPage();
        List<RegulatorName> list = regulatorNameService.selectRegulatorNameList(regulatorName);
        return getDataTable(list);
    }

    /**
     * 导出调节剂名称列表
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:export')")
    @Log(title = "调节剂名称", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RegulatorName regulatorName)
    {
        List<RegulatorName> list = regulatorNameService.selectRegulatorNameList(regulatorName);
        ExcelUtil<RegulatorName> util = new ExcelUtil<RegulatorName>(RegulatorName.class);
        return util.exportExcel(list, "调节剂名称数据");
    }

    /**
     * 获取调节剂名称详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:query')")
    @GetMapping(value = "/{regulatorNameId}")
    public AjaxResult getInfo(@PathVariable("regulatorNameId") Long regulatorNameId)
    {
        return AjaxResult.success(regulatorNameService.selectRegulatorNameById(regulatorNameId));
    }

    /**
     * 新增调节剂名称
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:add')")
    @Log(title = "调节剂名称", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RegulatorName regulatorName)
    {
        try {
            return toAjax(regulatorNameService.insertRegulatorName(regulatorName));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 修改调节剂名称
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:edit')")
    @Log(title = "调节剂名称", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RegulatorName regulatorName)
    {
        try {
            return toAjax(regulatorNameService.updateRegulatorName(regulatorName));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }
    }

    /**
     * 删除调节剂名称
     */
    @PreAuthorize("@ss.hasPermi('production:regulatorName:remove')")
    @Log(title = "调节剂名称", businessType = BusinessType.DELETE)
	@DeleteMapping("/{regulatorNameIds}")
    public AjaxResult remove(@PathVariable Long[] regulatorNameIds)
    {
        return toAjax(regulatorNameService.deleteRegulatorNameByIds(regulatorNameIds));
    }
}
